import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/Home'
import Test from '@/components/Test'
import Login from '@/components/Login'
import UserProfile from '@/components/UserProfile'
import Topic from '@/components/Topic'
import Setting from '@/components/Setting'
import Topics from '@/components/Topics'
import Abot from '@/components/Abot'
import Message from '@/components/Message'
import Collect from '@/components/Collect'


var newRouter = new Router({
  // mode: 'history',
  routes: [{
    path: '/',
    name: 'home',
    component: Home
  }, {
    path: '/login',
    name: 'login',
    component: Login
  }, {
    path: '/userProfile',
    name: 'userProfile',
    component: UserProfile
  }, {
    path: '/setting',
    name: 'setting',
    component: Setting
  }, {
    path: '/topic',
    name: 'topic',
    component: Topic
  },{
    path: '/topics',
    name: 'topics',
    component: Topics
  },{
    path: '/abot',
    name: 'abot',
    component: Abot
  },{
    path: '/message',
    name: 'message',
    component: Message
  },{
    path: '/collect',
    name: 'collect',
    component: Collect
  }, {
    path: '/Test',
    name: 'Test',
    component: Test
  }]
});

import permissions from '@/common/permissions';
newRouter.beforeEach((to, from, next) => {
  var isLogin = permissions.checkIsLogin();
  if (to.name != 'login' && to.name != 'home' && !isLogin) {
    next({ name: 'login' });
  } else {
    if (to.name == 'login' && isLogin) {
      next({ name: 'userProfile' });
    } else {
      next();
    }
  }
})

export default newRouter;
